// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: GHZ Setup Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//

private alias object.value0 : object.timer
private alias object.value1 : object.index

private alias object.value2 : object.drawX
private alias object.value3 : object.drawY
private alias object.value4 : object.letter
private alias object.value5 : object.charamount
private alias object.value6 : Object.MenuChoose

private alias object.value7 : object.fadetimer

private alias object.value8 : Object.TextState
private alias object.value9 : Object.Storage

private alias object.value10 : Object.TouchTimer
private alias object.value11 : object.TableContainer
private alias object.value12 : object.row
private alias object.value13 : object.tableholder
private alias object.value14 : object.rects

private alias object.value16 : object.loader
private alias object.value17 : object.bart
private alias object.value18 : object.TableContainer2
private alias object.value19 : object.store

private alias object.value20 : Info.State
private alias object.value21 : Info.Alpha

private alias object.value22 : Table.Insert

public value BoxPositionY = 0;
public value CharacterTalking = 0;
public value TextActive = 0;
public value CutsceneActive = 0;

public alias saveRAM[6010] : custom.freddy
public alias saveRAM[6011] : custom.bonnie
public alias saveRAM[6012] : custom.chica
public alias saveRAM[6013] : custom.foxy
public alias saveRAM[6014] : custom.golden
public alias saveRAM[6015] : custom.endo
public alias saveRAM[6016] : custom.puppet
public alias saveRAM[6017] : custom.ballboy
public alias saveRAM[6018] : custom.jirl
public alias saveRAM[6019] : custom.springtrap
public alias saveRAM[6020] : custom.shadowfred
public alias saveRAM[6021] : custom.shadowbon
public alias saveRAM[6022] : custom.toyfreddy
public alias saveRAM[6023] : custom.toybonnie
public alias saveRAM[6024] : custom.toychica
public alias saveRAM[6025] : custom.mango
public alias saveRAM[6026] : custom.withfreddy
public alias saveRAM[6027] : custom.withbonnie
public alias saveRAM[6028] : custom.withchica
public alias saveRAM[6029] : custom.withfoxy

public alias saveRAM[286] : saved.language

private table CustomNightRow1
	custom.freddy, custom.bonnie, custom.chica, custom.foxy, custom.golden, custom.endo, custom.puppet
end table

private table CustomNightRow2
	custom.ballboy, custom.jirl, custom.springtrap, custom.shadowfred, custom.shadowbon, custom.toyfreddy, custom.toybonnie
end table

private table CustomNightRow3
	custom.toychica, custom.mango, custom.withfreddy, custom.withbonnie, custom.withchica, custom.withfoxy
end table

reserve function TextDrawing
reserve function CustomNight_TouchRect
reserve function InfoOverlay

function InfoOverlay
	CheckTouchRect(0, 0, screen.xsize, screen.ysize)
	if checkResult == -1
		Object.TouchTimer = false
	end if
	if Object.TouchTimer == 0
		CheckTouchRect(0, 0, screen.xsize, screen.ysize)
		if CheckResult > -1
			Object.TouchTimer = 1
			inputpress.buttonb = 1
		end if
	end if
end function

function CustomNight_TouchRect
	CheckTouchRect(0, 0, screen.xsize, screen.ysize)
	if checkResult == -1
		Object.TouchTimer = false
	end if
	if Object.TouchTimer == 0


		object.row = 0
		object.TableContainer = 0
		object.rects = 0
		while object.row < 322
			//TOP
			//x value
			temp0 = object.rects
			temp0 += screen.xcenter
			temp0 -= 157
			temp1 = object.rects
			temp1 += screen.xcenter
			temp1 -= 119
			//y values
			temp4	= 48
			temp5	= 80

			//x, y, x, y
			CheckTouchRect(temp0, temp4, temp1, temp5)
			if CheckResult > -1
				Object.TouchTimer = 1
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow1)
				object.index = object.TableContainer
				object.drawY = 0
				object.charamount = 0
				PlaySfx(SfxName[Score Add], 0)	
				object.state++
			end if	


			//x value
			temp0 = object.rects
			temp0 += screen.xcenter
			temp0 -= 157
			temp1 = object.rects
			temp1 += screen.xcenter
			temp1 -= 119
			//y values
			temp4	+= 57
			temp5	+= 57

			//x, y, x, y
			CheckTouchRect(temp0, temp4, temp1, temp5)
			if CheckResult > -1
				Object.TouchTimer = 1
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow2)
				object.index = object.TableContainer
				object.index += 7
				object.drawY = 0
				object.charamount = 0
				PlaySfx(SfxName[Score Add], 0)	
				object.state++		
			end if	


			object.TableContainer++
			object.rects += 46
			object.row += 46
		loop

		object.row = 0
		object.TableContainer = 0
		object.rects = 0
		while object.row < 276
			//x value
			temp0 = object.rects
			temp0 += screen.xcenter
			temp0 -= 157
			temp1 = object.rects
			temp1 += screen.xcenter
			temp1 -= 119
			//y values
			temp4	= 48
			temp5	= 80

			//x value
			temp0 = object.rects
			temp0 += screen.xcenter
			temp0 -= 157
			temp1 = object.rects
			temp1 += screen.xcenter
			temp1 -= 119
			//y values
			temp4	+= 57
			temp5	+= 57

			//x value
			temp0 = object.rects
			temp0 += screen.xcenter
			temp0 -= 134
			temp1 = object.rects
			temp1 += screen.xcenter
			temp1 -= 96
			//y values
			temp4	+= 56
			temp5	+= 57

			//x, y, x, y
			CheckTouchRect(temp0, temp4, temp1, temp5)
			if CheckResult > -1
				Object.TouchTimer = 1
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow3)
				object.index = object.TableContainer
				object.index += 14
				object.drawY = 0
				object.charamount = 0
				PlaySfx(SfxName[Score Add], 0)	
				object.state++				
			end if	


			object.TableContainer++
			object.rects += 46
			object.row += 46
		loop


		object.row = 0
		object.TableContainer = 0
		object.rects = 0
		while object.row < 322
			//TOP
			//x value
			temp0 = object.rects
			temp0 += screen.xcenter
			temp0 -= 157
			temp1 = object.rects
			temp1 += screen.xcenter
			temp1 -= 139
			//y values
			temp4	= 84
			temp5	= 100

			//x, y, x, y
			CheckTouchRect(temp0, temp4, temp1, temp5)
			if CheckResult > -1
				Object.TouchTimer = 1
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow1)
				if Table.Insert > 0
					Table.Insert--
					PlaySfx(SfxName[Score Add], 0)	
				else
					PlaySfx(SfxName[Error], 0)
				end if
				SetTableValue(Table.Insert, object.TableContainer, CustomNightRow1)				
			end if	

			temp0 += 20
			temp1 += 20
			CheckTouchRect(temp0, temp4, temp1, temp5)
			if CheckResult > -1
				Object.TouchTimer = 1
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow1)
				if Table.Insert < 20
					Table.Insert++
					PlaySfx(SfxName[Score Add], 0)	
				else
					PlaySfx(SfxName[Error], 0)
				end if
				SetTableValue(Table.Insert, object.TableContainer, CustomNightRow1)			
			end if


			//x value
			temp0 = object.rects
			temp0 += screen.xcenter
			temp0 -= 157
			temp1 = object.rects
			temp1 += screen.xcenter
			temp1 -= 139
			//y values
			temp4	+= 57
			temp5	+= 57

			//x, y, x, y
			CheckTouchRect(temp0, temp4, temp1, temp5)
			if CheckResult > -1
				Object.TouchTimer = 1
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow2)
				if Table.Insert > 0
					Table.Insert--
					PlaySfx(SfxName[Score Add], 0)	
				else
					PlaySfx(SfxName[Error], 0)
				end if
				SetTableValue(Table.Insert, object.TableContainer, CustomNightRow2)				
			end if	

			temp0 += 20
			temp1 += 20
			CheckTouchRect(temp0, temp4, temp1, temp5)
			if CheckResult > -1
				Object.TouchTimer = 1
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow2)
				if Table.Insert < 20
					Table.Insert++
					PlaySfx(SfxName[Score Add], 0)	
				else
					PlaySfx(SfxName[Error], 0)
				end if
				SetTableValue(Table.Insert, object.TableContainer, CustomNightRow2)	
			end if
			object.TableContainer++
			object.rects += 46
			object.row += 46
		loop


		object.row = 0
		object.TableContainer = 0
		object.rects = 0
		while object.row < 276
			//x value
			temp0 = object.rects
			temp0 += screen.xcenter
			temp0 -= 157
			temp1 = object.rects
			temp1 += screen.xcenter
			temp1 -= 139
			//y values
			temp4	= 84
			temp5	= 100

			//x value
			temp0 = object.rects
			temp0 += screen.xcenter
			temp0 -= 157
			temp1 = object.rects
			temp1 += screen.xcenter
			temp1 -= 139
			//y values
			temp4	+= 57
			temp5	+= 57

			//x value
			temp0 = object.rects
			temp0 += screen.xcenter
			temp0 -= 134
			temp1 = object.rects
			temp1 += screen.xcenter
			temp1 -= 116
			//y values
			temp4	+= 56
			temp5	+= 57

			//x, y, x, y
			CheckTouchRect(temp0, temp4, temp1, temp5)
			if CheckResult > -1
				Object.TouchTimer = 1
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow3)
				if Table.Insert > 0
					Table.Insert--
					PlaySfx(SfxName[Score Add], 0)	
				else
					PlaySfx(SfxName[Error], 0)
				end if
				SetTableValue(Table.Insert, object.TableContainer, CustomNightRow3)				
			end if	

			temp0 += 20
			temp1 += 20
			CheckTouchRect(temp0, temp4, temp1, temp5)
			if CheckResult > -1
				Object.TouchTimer = 1
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow3)
				if Table.Insert < 20
					Table.Insert++
					PlaySfx(SfxName[Score Add], 0)	
				else
					PlaySfx(SfxName[Error], 0)
				end if
				SetTableValue(Table.Insert, object.TableContainer, CustomNightRow3)			
			end if
			object.TableContainer++
			object.rects += 46
			object.row += 46
		loop
		//BOTTOM
		//set values!

		//x value
		temp0 = object.rects
		temp0 += screen.xcenter
		temp0 -= 287
		temp1 = object.rects
		temp1 += screen.xcenter
		temp1 -= 273
		//x, y, x, y
		CheckTouchRect(temp0, 218, temp1, 230)
		if CheckResult > -1

			PlaySfx(SfxName[Lamp Post], 0)

			Object.TouchTimer = 1
			object.TableContainer = 0
			while object.TableContainer < 7
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow1)
				Table.Insert = 0
				SetTableValue(Table.Insert, object.TableContainer, CustomNightRow1)	
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow2)
				Table.Insert = 0
				SetTableValue(Table.Insert, object.TableContainer, CustomNightRow2)					
				object.TableContainer++
			loop

			object.TableContainer = 0
			while object.TableContainer < 6
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow3)
				Table.Insert = 0
				SetTableValue(Table.Insert, object.TableContainer, CustomNightRow3)	
				object.TableContainer++
			loop
		end if	

		//x value
		temp0 = object.rects
		temp0 += screen.xcenter
		temp0 -= 272
		temp1 = object.rects
		temp1 += screen.xcenter
		temp1 -= 258
		//x, y, x, y
		CheckTouchRect(temp0, 218, temp1, 230)
		if CheckResult > -1

			PlaySfx(SfxName[Lamp Post], 0)

			Object.TouchTimer = 1
			object.TableContainer = 0
			while object.TableContainer < 7
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow1)
				Table.Insert = 5
				SetTableValue(Table.Insert, object.TableContainer, CustomNightRow1)	
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow2)
				Table.Insert = 5
				SetTableValue(Table.Insert, object.TableContainer, CustomNightRow2)					
				object.TableContainer++
			loop

			object.TableContainer = 0
			while object.TableContainer < 6
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow3)
				Table.Insert = 5
				SetTableValue(Table.Insert, object.TableContainer, CustomNightRow3)	
				object.TableContainer++
			loop
		end if


		//x value
		temp0 = object.rects
		temp0 += screen.xcenter
		temp0 -= 257
		temp1 = object.rects
		temp1 += screen.xcenter
		temp1 -= 237
		//x, y, x, y
		CheckTouchRect(temp0, 218, temp1, 230)
		if CheckResult > -1
			PlaySfx(SfxName[Lamp Post], 0)
			Object.TouchTimer = 1
			object.TableContainer = 0
			while object.TableContainer < 7
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow1)
				Table.Insert = 10
				SetTableValue(Table.Insert, object.TableContainer, CustomNightRow1)	
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow2)
				Table.Insert = 10
				SetTableValue(Table.Insert, object.TableContainer, CustomNightRow2)					
				object.TableContainer++
			loop
			object.TableContainer = 0
			while object.TableContainer < 6
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow3)
				Table.Insert = 10
				SetTableValue(Table.Insert, object.TableContainer, CustomNightRow3)	
				object.TableContainer++
			loop
		end if


		//x value
		temp0 = object.rects
		temp0 += screen.xcenter
		temp0 -= 236
		temp1 = object.rects
		temp1 += screen.xcenter
		temp1 -= 216
		//x, y, x, y
		CheckTouchRect(temp0, 218, temp1, 230)
		if CheckResult > -1
			PlaySfx(SfxName[Lamp Post], 0)
			Object.TouchTimer = 1
			object.TableContainer = 0
			while object.TableContainer < 7
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow1)
				Table.Insert = 15
				SetTableValue(Table.Insert, object.TableContainer, CustomNightRow1)	
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow2)
				Table.Insert = 15
				SetTableValue(Table.Insert, object.TableContainer, CustomNightRow2)					
				object.TableContainer++
			loop
			object.TableContainer = 0
			while object.TableContainer < 6
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow3)
				Table.Insert = 15
				SetTableValue(Table.Insert, object.TableContainer, CustomNightRow3)	
				object.TableContainer++
			loop
		end if



		//x value
		temp0 = object.rects
		temp0 += screen.xcenter
		temp0 -= 215
		temp1 = object.rects
		temp1 += screen.xcenter
		temp1 -= 194
		//x, y, x, y
		CheckTouchRect(temp0, 218, temp1, 230)
		if CheckResult > -1
			PlaySfx(SfxName[Lamp Post], 0)
			Object.TouchTimer = 1
			object.TableContainer = 0
			while object.TableContainer < 7
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow1)
				Table.Insert = 20
				SetTableValue(Table.Insert, object.TableContainer, CustomNightRow1)	
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow2)
				Table.Insert = 20
				SetTableValue(Table.Insert, object.TableContainer, CustomNightRow2)					
				object.TableContainer++
			loop
			object.TableContainer = 0
			while object.TableContainer < 6
				GetTableValue(Table.Insert, object.TableContainer, CustomNightRow3)
				Table.Insert = 20
				SetTableValue(Table.Insert, object.TableContainer, CustomNightRow3)	
				object.TableContainer++
			loop
		end if

		temp0 = object.rects
		temp0 += screen.xsize
		temp0 -= 315
		temp1 = object.rects
		temp1 += screen.xsize
		temp1 -= 285
		//x, y, x, y
		CheckTouchRect(temp0, 8, temp1, 40)
		if CheckResult > -1
			PlaySfx(SfxName[Lamp Post], 0)
			Object.TouchTimer = 1
			inputPress.start = 1
		end if	
		
		temp0 = object.rects
		temp0 += screen.xsize
		temp0 -= 347
		temp1 = object.rects
		temp1 += screen.xsize
		temp1 -= 317
		//x, y, x, y
		CheckTouchRect(temp0, 8, temp1, 40)
		if CheckResult > -1
			PlaySfx(SfxName[Skidding], 0)
			Object.TouchTimer = 1
			inputPress.buttonB = 1
		end if				

	end if
end function

function TextDrawing
	temp0 = object.drawX
	temp1 = object.charamount
	while temp1 > 0
	// Get the actual character at the current char pos
	GetTextInfo(object.letter, 1, TEXTINFO_TEXTDATA, Object.Index, ArrayPos1)

	Object.Frame = 0
	
	// Take uppercase letters and turn them into a matching Sprite Frame
	if object.letter > 64
		if object.letter < 91
			Object.Frame  = object.letter
			Object.Frame -= 64
		end if
	end if
	
	// Take lowercase letters and turn them into a matching Sprite Frame
	if object.letter > 96
		if object.letter < 123
			Object.Frame  = object.letter
			Object.Frame -= 96
		end if
	end if

		//ASCII Table required!
		switch object.letter
			case 33	//!
				ArrayPos1++
				temp1--
				object.drawX += 0x69
				break		

			case 39	;DrawSpriteScreenFX(84, FX_INK, object.drawX, object.drawY) ;ArrayPos1++ ;temp1-- ;object.drawX += 0x8 ;break	//''
			case 44	;DrawSpriteScreenFX(55, FX_INK, object.drawX, object.drawY) ;ArrayPos1++ ;temp1-- ;object.drawX += 0x8 ;break	//0
			case 48	;DrawSpriteScreenFX(44, FX_INK, object.drawX, object.drawY) ;ArrayPos1++ ;temp1-- ;object.drawX += 0x8 ;break	//0
			case 49	;DrawSpriteScreenFX(45, FX_INK, object.drawX, object.drawY) ;ArrayPos1++ ;temp1-- ;object.drawX += 0x8 ;break	//1
			case 50	;DrawSpriteScreenFX(46, FX_INK, object.drawX, object.drawY) ;ArrayPos1++ ;temp1-- ;object.drawX += 0x8 ;break	//2
			case 51	;DrawSpriteScreenFX(47, FX_INK, object.drawX, object.drawY) ;ArrayPos1++ ;temp1-- ;object.drawX += 0x8 ;break	//3
			case 52	;DrawSpriteScreenFX(48, FX_INK, object.drawX, object.drawY) ;ArrayPos1++ ;temp1-- ;object.drawX += 0x8 ;break	//4
			case 53	;DrawSpriteScreenFX(49, FX_INK, object.drawX, object.drawY) ;ArrayPos1++ ;temp1-- ;object.drawX += 0x8 ;break	//4
			case 54	;DrawSpriteScreenFX(50, FX_INK, object.drawX, object.drawY) ;ArrayPos1++ ;temp1-- ;object.drawX += 0x8 ;break	//4
			case 55	;DrawSpriteScreenFX(51, FX_INK, object.drawX, object.drawY) ;ArrayPos1++ ;temp1-- ;object.drawX += 0x8 ;break	//4
			case 56	;DrawSpriteScreenFX(52, FX_INK, object.drawX, object.drawY) ;ArrayPos1++ ;temp1-- ;object.drawX += 0x8 ;break	//4
			case 57	;DrawSpriteScreenFX(53, FX_INK, object.drawX, object.drawY) ;ArrayPos1++ ;temp1-- ;object.drawX += 0x8 ;break	//4
			case 58	
				DrawSpriteScreenFX(54, FX_INK, object.drawX, object.drawY) 
				ArrayPos1++ 
				temp1-- 
				object.drawX += 0x8 
			break	//4
			//Accent Marks
			case 64	//@	LINE SKIPPER
				object.drawY += 12

			

				// Move to the next letter
				ArrayPos1++
				
				// And decrease the remaining letter count, as well
				temp1--
				
				// Each letter should be spaced 8 pixels apart
				switch object.index
				case 2
					object.drawX -= 178
				break
				case 3
					object.drawX -= 174
				break
				default
					object.drawX -= 184
				break
				end switch
			break
			case 36	//$	LINE SKIPPER
				object.drawY += 12
				object.drawX = temp0
			

				// Move to the next letter
				ArrayPos1++
				
				// And decrease the remaining letter count, as well
				temp1--
				
				// Each letter should be spaced 8 pixels apart
				object.drawX += 48
			break			
			case 35	//@	LINE SKIPPER
				object.drawY += 12

				// Move to the next letter
				ArrayPos1++
				
				// And decrease the remaining letter count, as well
				temp1--
				
				// Each letter should be spaced 8 pixels apart
				switch object.index
				case 2
					object.drawX += 180
				break				
				case 3
					object.drawX += 184
				break
				case 4
					object.drawX += 155
				break
				case 5
					object.drawX += 180
				break				
				case 7
					object.drawX += 160
				break
				case 8
					object.drawX += 198
				break
				case 9
					object.drawX += 165
				break
				case 10
					object.drawX += 155
				break
				case 11	//shadow bonnie
					object.drawX += 165
				break			
				case 12	//toy freddy
				case 13
					object.drawX += 165
				break		
				case 14
					object.drawX += 170
				break
				case 16
				case 17
					object.drawX += 145
				break
				case 19
					object.drawX += 155
				break
				case 18
					object.drawX += 150
				break
				default
					object.drawX += 184
				break
				end switch
			break			
			default
			// Notably, the space character (32) is skipped over here, as it shouldn't draw anything
			
			DrawSpriteScreenFX(Object.Frame, FX_INK, object.drawX, object.drawY)
			

			// Move to the next letter
			ArrayPos1++
			
			// And decrease the remaining letter count, as well
			temp1--
			
			// Each letter should be spaced 8 pixels apart
			object.drawX += 0x8
			break
		end switch
	loop
end function

//if being stupid was a job

event ObjectMain
	if stage.actnum != 2
		object[4].type = TypeName[Blank Object]		
	end if

	switch object.state
	case 0
		object.timer = 255
		object.state++
	break
	case 1	//fade in
		Info.Alpha = 0
		if object.timer > -1
			object.timer -= 8
		else
			PlayMusic(1)
			object.timer = 0	
			object.state++
		end if
		SetClassicFade(0, 0, 0, Object.Timer)	
	break
	case 2
		Callfunction(CustomNight_TouchRect)

		if inputpress.buttonB == 1
			object.state = 99
		end if		

		if inputpress.start == 1
			PlaySfx(SfxName[Lamp Post], 0)
			object.state = 98
		end if			
	break
	case 3
		if Info.Alpha < 192
			Info.Alpha += 8
		else
			info.alpha = 192
		end if
		if object.alpha < 255
			object.alpha += 8
		else
			object.alpha = 255
			Callfunction(InfoOverlay)
		end if

		if object.drawY < screen.ycenter
			object.drawY++
		else
			object.drawY = screen.ycenter
		end if


			if inputpress.buttonB == 1
				object.state++
			end if
		
	break
	case 4
		if Info.Alpha > 192
			Info.Alpha -= 2
		else
			Info.Alpha = 0
		end if
		if object.alpha > 255
			object.alpha -= 2
		else
			object.alpha = 0
		end if

		if object.drawY >= screen.ycenter
			object.drawY--
		else
			object.drawY = 0
		end if

		if object.alpha == 0
			object.charamount = 0
			object.state = 2
		end if

	break
	case 98 //PLAY GAME
	
		//6010 to 16
		object.TableContainer2 = 0
		arrayPos0 = 6010
		while object.TableContainer2 < 7
			GetTableValue(Table.Insert, object.TableContainer2, CustomNightRow1)
			saveRAM[arrayPos0] = Table.Insert
			object.TableContainer2++
			arrayPos0++
		loop

		//6017 to 23
		object.TableContainer2 = 0
		arrayPos0 = 6017
		while object.TableContainer2 < 7
			GetTableValue(Table.Insert, object.TableContainer2, CustomNightRow2)
			saveRAM[arrayPos0] = Table.Insert
			object.TableContainer2++
			arrayPos0++
		loop

		//6024 to idk
		object.TableContainer2 = 0
		arrayPos0 = 6024
		while object.TableContainer2 < 6
			GetTableValue(Table.Insert, object.TableContainer2, CustomNightRow3)
			saveRAM[arrayPos0] = Table.Insert
			object.TableContainer2++
			arrayPos0++
		loop

		WriteSaveRAM()	
		options.gameMode = 1
		music.volume -= 8
		object.timer += 8
		SetClassicFade(0, 0, 0, Object.timer)
		if object.timer >= 255
			options.gameMode = 1
			music.volume = 0
			StopMusic()
			stage.activeList = BONUS_STAGE
			stage.listPos = 10
			LoadStage()				
		end if
	break	
	case 99 //EXIT FREDDY

		//6010 to 16
		object.TableContainer2 = 0
		arrayPos0 = 6010
		while object.TableContainer2 < 7
			GetTableValue(Table.Insert, object.TableContainer2, CustomNightRow1)
			saveRAM[arrayPos0] = 0
			object.TableContainer2++
			arrayPos0++
		loop

		//6017 to 23
		object.TableContainer2 = 0
		arrayPos0 = 6017
		while object.TableContainer2 < 7
			GetTableValue(Table.Insert, object.TableContainer2, CustomNightRow2)
			saveRAM[arrayPos0] = 0
			object.TableContainer2++
			arrayPos0++
		loop

		//6024 to idk
		object.TableContainer2 = 0
		arrayPos0 = 6024
		while object.TableContainer2 < 6
			GetTableValue(Table.Insert, object.TableContainer2, CustomNightRow3)
			saveRAM[arrayPos0] = 0
			object.TableContainer2++
			arrayPos0++
		loop

		WriteSaveRAM()

		music.volume -= 8
		object.timer += 8
		SetClassicFade(0, 0, 0, Object.timer)
		if object.timer >= 255
			music.volume = 0
			StopMusic()
			stage.activeList = BONUS_STAGE
			stage.listPos = 3
			LoadStage()				
		end if
	break
	end switch


	switch Object.TextState
	case 0
		Object.Index = -1
		object.charamount = 0
		Object.TextState++
	break
	case 1
		GetTextInfo(object.store, 1, TEXTINFO_TEXTSIZE, Object.Index, ArrayPos1)
	
		if object.charamount != object.store
			object.charamount++
		end if
	break
	end switch
end event

event ObjectDraw
	DrawRect(0, 0, screen.xsize, screen.ysize, 0, 0, color1, 255)

	DrawRect(0, 128, screen.xsize, screen.ysize, color1, color1, color2, 255)

	DrawRect(0, 192, screen.xsize, screen.ysize, color2, color2, color3, 255)
	DrawRect(0, 224, screen.xsize, screen.ysize, color3, color3, color4, 255)

	DrawRect(0, 233, screen.xsize, 8, 0, 0, 0, 255)	
	DrawRect(0, 0, screen.xsize, 8, 0, 0, 0, 255)	
	DrawRect(0, 0, 8, screen.ysize, 0, 0, 0, 255)	
	temp3 = screen.xsize
	temp3 -= screen.xsize
	temp3 += screen.xcenter
	temp3 *= 2
	temp3 -= 8
	DrawRect(temp3, 0, 8, screen.ysize, 0, 0, 0, 255)




	temp3 = screen.xsize
	temp3 -= screen.xsize
	temp3 += screen.xcenter
	temp3 -= 154
	DrawSpriteScreenXY(56,  temp3, 48)
	
	temp3 += 74	
	DrawSpriteScreenXY(58,  temp3, 218)

	DrawSpriteScreenXY(59,  8, 12)
	DrawSpriteScreenXY(81,  8, 24)	//info


	//DrawNumbers(int startingFrame, int iXPos, int iYPos, int value, int digitCnt, int spacing, bool showAllDigits)
	

	temp3 = screen.xsize
	temp3 -= screen.xsize
	temp3 += screen.xcenter
	temp3 -= 135
	temp4 = object.value15
	temp4 += screen.xcenter
	temp4 += 146
	object.TableContainer = 0
	while temp3 < temp4	
		GetTableValue(Table.Insert, object.TableContainer, CustomNightRow1)
		DrawNumbers(44, temp3, 87, Table.Insert, 2, 8, 1)

		GetTableValue(Table.Insert, object.TableContainer, CustomNightRow2)
		DrawNumbers(44, temp3, 143, Table.Insert, 2, 8, 1)

		temp3 += 46
		object.TableContainer++
	loop


	temp3 = screen.xsize
	temp3 -= screen.xsize
	temp3 += screen.xcenter
	temp3 -= 112
	temp4 = object.value15
	temp4 += screen.xcenter
	temp4 += 120
	object.TableContainer = 0
	while temp3 < temp4	
		GetTableValue(Table.Insert, object.TableContainer, CustomNightRow3)
		DrawNumbers(44, temp3, 199, Table.Insert, 2, 8, 1)

		temp3 += 46
		object.TableContainer++
	loop


	temp1 = screen.xsize
	temp1 -= 40
	DrawSpriteScreenXY(82, temp1, 8)
	temp1 -= 32
	DrawSpriteScreenXY(83, temp1, 8)

	switch object.state
	case 3
	case 4
		DrawRect(0, 0, screen.xsize, screen.ysize, 0, 0, color1, Info.Alpha)

		temp6 = object.index// = object.TableContainer
		temp6 += 60
		DrawSpriteScreenFX(temp6, FX_INK, screen.xcenter, screen.ycenter)
	break
	end switch

	object.inkEffect = INK_ALPHA

	ArrayPos1 = 0	//starting letter
	
	temp3 = screen.xsize
	temp3 -= screen.xsize
	temp3 += screen.xcenter
	temp3 -= 204

	object.drawX = temp3




	Callfunction(TextDrawing)	

	//mouse
	DrawSpriteScreenXY(57, touchscreen[0].xpos, touchscreen[0].ypos)

	switch object.state
	case 0
		DrawRect(0, 0, screen.xsize, screen.ysize, 0, 0, 0, 255)
	break
	case 1
		DrawRect(0, 0, screen.xsize, screen.ysize, 0, 0, 0, object.timer)
	break	
	end switch
end event

event ObjectStartup
	
	LoadSpriteSheet("FNAF/Objects1.gif")

	switch saved.language
	case 1
		LoadTextFile(1, "Data/Game/Portuguese/CustomNight.txt")
	break	
	case 3
		LoadTextFile(1, "Data/Game/French/CustomNightFR.txt")
	break
	default	//Default English
		LoadTextFile(1, "Data/Game/CustomNight.txt")
	break
	end switch

	SetMusicTrack("CustomNight.ogg", 1, 1)

	SpriteFrame(0, 0, 0, 0, 0, 0)	//0 the bar

	// #1 -> #34 - Alphabet
	// (Using loops to automate Sprite Framing)

	//not related too stuff in objectdraw

	//temp7: starting ypos, progresses by 17 and stops before 100, because theres only 3 rows
	//temp6: starting xpos, the letter A
	temp1 = 49
	while temp1 < 100
		temp0 = 549
		while temp0 < 648
			SpriteFrame(-3, -6, 8, 16, temp0, temp1)
			temp0 += 9
		loop
		temp1 += 17
	loop

	//fred head
	SpriteFrame(-3, -6, 64, 64, 513, 117) //35

	//options
	SpriteFrame(-3, -6, 64, 57, 888, 1) //36

	//continue, new game, main menu
	SpriteFrame(-3, -6, 64, 11, 956, 1) //37
	SpriteFrame(-3, -6, 64, 11, 956, 24) //38
	SpriteFrame(-3, -6, 64, 11, 956, 47) //39

	SpriteFrame(-3, -6, 202, 217, 821, 560) //40

	SpriteFrame(-3, -6, 40, 7, 847, 1) //41	

	SpriteFrame(-3, -6, 91, 34, 888, 59) //42

	SpriteFrame(-3, -6, 91, 11, 888, 94) //43

	SpriteFrame(-3, -6, 91, 11, 888, 117) //44

	//#44 - idk numbers
	temp1 = 49
	while temp1 < 100
		temp0 = 513
		while temp0 < 549
			SpriteFrame(-3, -6, 8, 16, temp0, temp1)
			temp0 += 9
		loop
		temp1 += 17
	loop

	SpriteFrame(0, 0, 308, 160, 284, 468) //56

	// 57 - mice
	SpriteFrame(0, 0, 12, 21, 513, 1)

	//set	58
	SpriteFrame(0, 0, 162, 10, 284, 457)
	//59
	SpriteFrame(0, 0, 95, 7, 447, 460)


	//60-80
	temp1 = 618
	while temp1 < 714
		temp0 = 2
		while temp0 < 226
			SpriteFrame(-16, -32, 32, 32, temp0, temp1)
			temp0 += 32
		loop
		temp1 += 32
	loop

	//81
	SpriteFrame(0, 0, 83, 10, 2, 390)	
	SpriteFrame(0, 0, 32, 32, 227, 618)	//82
	SpriteFrame(0, 0, 32, 32, 227, 651)	//83

	SpriteFrame(-3, -6, 8, 16, 549, 100) //84

	//85 
	temp4 = 0
	while temp4 < 56
		SpriteFrame(-3, -6, 8, 16, 549, 100) //84
		temp4++
	loop

	// // Place the HUD object into reserved object slot 4 and initialise its values
		Object[0].Type = TypeName[Custom Night]
	
	// // The object should always be run, regardless if it's within camera bounds or not
		Object[0].Priority = PRIORITY_ACTIVE
	
	// // Make the HUD draw ontop of all other sprites, too
		Object[0].DrawOrder = 5

end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("IIZ/Objects1.gif")
	SpriteFrame(0, 0, 64, 64, 153, 154)
end event
